import { NodeStatus } from "@/enum/NodeStatus.ts"
import { NodeType } from "@/enum/NodeType.ts"
import { MsgType } from '@/enum/MsgType.ts'

/**
 * 节点数据
 */
export interface NodeData<T = any> {
    id: string,
    title: string,
    type: string,
    status: string,
    data: T
}

/**
 * 节点图标
 */
interface NodeIcon{
    icon:string,
    type:string
}

/**
 * 节点图标列表
 */
const nodeIconList: NodeIcon[] = [
    {
        icon: new URL('../assets/git-img.png', import.meta.url).href,
        type: NodeType.GIT,
    },
    {
        icon: new URL('../assets/maven-img.png', import.meta.url).href,
        type: NodeType.MAVEN,
    },
    {
        icon: new URL('../assets/docker-img.png', import.meta.url).href,
        type: NodeType.DOCKER,
    },
    {
        icon: new URL('../assets/k8s-img.png', import.meta.url).href,
        type: NodeType.K8S,
    },
    {
        icon: new URL('../assets/ssh-img.png', import.meta.url).href,
        type: NodeType.SSH,
    },
    {
        icon: new URL('../assets/sftp-img.png', import.meta.url).href,
        type: NodeType.SFTP,
    },
    {
        icon: new URL('../assets/nodejs-img.png', import.meta.url).href,
        type: NodeType.NODEJS,
    },
    {
        icon: new URL('../assets/gradle-img.png', import.meta.url).href,
        type: NodeType.GRADLE,
    },
    {
        icon: new URL('../assets/oss-img.png', import.meta.url).href,
        type: NodeType.OSS,
    }
]

/**
 * 获取节点图标
 * @param type 节点类型
 * @returns 返回节点图标
 */
export const getNodeIcon = (type:string):string => {
    const data = nodeIconList.find(i => i.type == type)
    return data ? data.icon : ''
}

/**
 * 节点状态颜色
 */
interface NodeStatusColor{
    status:string,
    color:string
}

/**
 * 状态颜色列表
 */
const statusColorList: NodeStatusColor[] = [
    {
        status: NodeStatus.INIT,
        color: '#409EFF'
    },
    {
        status: NodeStatus.RUN,
        color: '#E6A23C'
    },
    {
        status: NodeStatus.SUCCESS,
        color: '#67C23A'
    },
    {
        status: NodeStatus.ERROR,
        color: '#F56C6C'
    }
]

/**
 * 获取节点状态颜色
 * @param status 节点状态
 * @returns
 */
export const getStatusColor = (status:string):string => {
    if(!status){
        return '#409EFF'
    }

    const statusColor = statusColorList.find(i => i.status == status)
    return statusColor ? statusColor.color : '#409EFF'
}

export interface SecretData{
    name: string,
    type: string,
    secretKey: string,
    secretValue: string
}

export interface GitData{
    url: string,
    secretName: string,
    defaultBranch: string,
    branch: string
}

export interface MavenData{
    command: string,
    pomPath: string,
    dockerImage: string,
    workDir: string
}

export interface DockerData{
    name: string,
    version: string,
    registry: string,
    dockerFile: string,
    buildPath: string,
    secretName: string,
    afterCmd: string,
    workDir: string
}

export interface K8sData{
    apiServer: string,
    clientCertificateData: string,
    clientKeyData: string,
    filePath: string,
    workDir: string
}

export interface NodeJsData{
    dockerImage: string,
    command: string,
    workDir: string
}

/**
 * 节点模版数据
 */
export const nodeTemplateList: Array<NodeData<any>> = [
    {
        id: '',
        title: '克隆代码',
        type: NodeType.GIT,
        status: NodeStatus.INIT,
        data: {
            url: '',
            secretName: '',
            defaultBranch: 'master',
            branch: ''
        }
    },
    {
        id: '',
        title: '打包代码',
        type: NodeType.MAVEN,
        status: NodeStatus.INIT,
        data: {
            command: 'mvn clean package -U -DskipTests',
            pomPath: '',
            dockerImage: '',
            workDir: ''
        }
    },
    {
        id: '',
        title: '构建镜像',
        type: NodeType.DOCKER,
        status: NodeStatus.INIT,
        data: {
            name: '',
            version: '',
            registry: '',
            dockerFile: 'Dockerfile',
            buildPath: '.',
            secretName: '',
            afterCmd: 'docker builder prune -f && docker rmi $(docker images -f "dangling=true" -q)',
            workDir: ''
        }
    },
    {
        id: '',
        title: '部署k8s',
        type: NodeType.K8S,
        status: NodeStatus.INIT,
        data: {
            apiServer: 'https://localhost:6443',
            clientCertificateData: '',
            clientKeyData: '',
            filePath: '#workspace#/deploy.yaml',
            workDir: ''
        }
    },
    {
        id: '',
        title: 'nodejs构建',
        type: NodeType.NODEJS,
        status: NodeStatus.INIT,
        data: {
            dockerImage: '',
            command:'pnpm i && pnpm run build',
            workDir: ''
        }
    },
    {
        id: '',
        title: '执行ssh命令',
        type: NodeType.SSH,
        status: NodeStatus.INIT,
        data: {
            secretName: '',
            command: 'pwd',
            singleCommand: ''
        }
    },
    {
        id: '',
        title: '上传文件',
        type: NodeType.SFTP,
        status: NodeStatus.INIT,
        data: {
            secretName: '',
            remotePath: '',
            localPath: '',
            fileName: '',
            beforeCommand: ''
        }
    },
    {
        id: '',
        title: 'gradle构建',
        type: NodeType.GRADLE,
        status: NodeStatus.INIT,
        data: {
            command:''
        }
    },
    {
        id: '',
        title: 'oss上传',
        type: NodeType.OSS,
        status: NodeStatus.INIT,
        data: {
            path:''
        }
    }
]

export const getMsgColor = (type:string):string => {
    if(MsgType.INFO == type){
        return '#ffffff'
    }
    if(MsgType.ERROR == type){
        return '#F56C6C'
    }
    if(MsgType.SUCCESS == type){
        return '#67C23A'
    }

    return '#ffffff'
}

export interface MsgData<T = any>{
    type:string,
    msg:string,
    time:string,
    data:T
}

export interface NodeStatusData{
    id:string,
    status:string
}

export interface FlowStatusData{
    taskId:string,
    status:boolean
}